<template>
  <el-form ref="initFormRef" :model="initForm" :rules="rules" size="large" label-width="100px">
    <el-form-item label="机器代码:" prop="machinecode">
      <el-input v-model="initForm.machinecode" placeholder="机器代码" :disabled="true"> </el-input>
    </el-form-item>

    <el-form-item prop="license" label="license:">
      <el-input v-model="initForm.license" placeholder="license"> </el-input>
    </el-form-item>
  </el-form>
  <div class="login-btn">
    <el-button round size="large" type="primary" @click="saveconfig"> 保存 </el-button>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { Init } from '@api/init/interface'
import { getserialnumber, upconfigtodb } from '@api/init'
import type { ElForm } from 'element-plus'
import { ElMessage, FormInstance } from 'element-plus'

const props = defineProps({
  projectid: Number
})

const initForm = reactive<Init.ResupInitlist>({
  id: 0,
  license: '',

  machinecode: ''
})

const rules = reactive({
  license: [{ required: true, message: '请填写license' }]
})

const getmachinecode = async () => {
  let data = await getserialnumber()

  if (Number(data.code) == 0) {
    initForm.machinecode = String(data.data)
  }
}
const initFormRef = ref<FormInstance>()
const saveconfig = () => {
  initFormRef.value!.validate(async valid => {
    if (!valid) return
    try {
      initForm.id = Number(props.projectid)
      await upconfigtodb(initForm)
      ElMessage.success({ message: `初始化成功！` })
    } catch (error) {
      console.log(error)
    }
  })
}
onMounted(() => {
  getmachinecode()
})
</script>

<style scoped lang="scss">
@import '../index.scss';
</style>
